#pragma once

#include <QObject>

class QConnectionObjectEx : public QObject
{
	Q_DECLARE_PRIVATE(QObject)
public:
	inline bool isSender(const QObject* receiver, const char* signal) const
	{ return d_func()->isSender(receiver, signal); }
	inline QObjectList receiverList(const char* signal) const
	{ return d_func()->receiverList(signal); }
	inline QObjectList senderList() const
	{ return d_func()->senderList(); }
	const QObjectPrivate* dFunc() { return d_func(); }
};

template<typename T>
inline decltype((*(T*)0)((QObject*)0)) findFirstThat(QObject* obj, const char* slot, T fun)
{
	if (0 == obj)
	{
		return 0;
	}

	QObjectList receivers = ((QConnectionObjectEx*) obj)->receiverList(slot);
	Q_FOREACH(QObject * item, receivers)
	{
		auto result = fun(item);
		if (result != 0)
		{
			return result;
		}
	}

	return 0;
}
